/*
 *  TestGazetteer.java
 *
 */

package edu.columbia.gate.plugins;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;

//import com.sun.xml.internal.ws.util.QNameMap;


import junit.framework.*;

import gate.*;
import gate.corpora.DocumentContentImpl;
import gate.corpora.TestDocument;
import gate.util.GateRuntimeException;

public class TestEditDistanceGazetteer extends TestCase
{

	private static final boolean DEBUG = true;

	public TestEditDistanceGazetteer(String name)
	{
		super(name);
	}

	/** Fixture set up */
	public void setUp() throws Exception
	{
		//make sure the right plugin is loaded

		Gate.init();
		
		File pluginsHome = new File(System
				.getProperty(GateConstants.GATE_HOME_PROPERTY_NAME), "plugins");

		try
		{
			URL pluginsUrl = new File(pluginsHome, "Tools").toURI().toURL();
			URL pluginsUrl2 = new File(
					"C:\\SVN\\StrayVoltage\\GATE\\Plugins\\",
					"GatePlugins").toURI().toURL();

			Gate.getCreoleRegister().registerDirectories(pluginsUrl2);
		} catch (Exception e)
		{
			throw new GateRuntimeException(e);
		}
	}

	public void tearDown() throws Exception
	{
	} // tearDown

	/** Test the default tokeniser */
	public void testEditDistanceGazetteer() throws Exception
	{
		//get a document
		Document doc = Factory
				.newDocument(new File("C:\\SVN\\StrayVoltage\\GATE\\ProcessingResources", "SampleTicketTest3.xml").toURI().toURL());

		AnnotationSet annotSet = doc.getAnnotations();
		annotSet.add(new Long(0), new Long(doc.getContent().size() - 1), "FreeText", Factory.newFeatureMap());
		//create a default gazetteer
		EditDistanceFullTextGazetteer gaz = (EditDistanceFullTextGazetteer) Factory
				.createResource("edu.columbia.gate.plugins.EditDistanceFullTextGazetteer");
		
		//runtime stuff
		gaz.setDocument(doc);
		gaz.setAnnotationSetName("");
		gaz.setListsURL(new File("../../ProcessingResources/BlowFireGazetteer", "EditDistanceNormalizing.def").toURI().toURL());

		Date time = new Date();		
		gaz.execute();
		System.out.println("Ticket processed in " + (new Date().getTime() - time.getTime()) + " ms.");
		AnnotationSet resultAS = doc.getAnnotations();
		Annotation[] annots = new Annotation[resultAS.size()];
		resultAS.toArray(annots);
		for(int i=0; i<annots.length; i++)
		{
			SimpleAnnotation annot = (SimpleAnnotation) annots[i];
			long textStart = annot.getStartNode().getOffset().intValue();
			long textEnd = annot.getEndNode().getOffset().intValue();
			String normalString = ((String) annot.getFeatures().get("entryMatched"));
			if(normalString != null)
			{
				System.out.println(doc.getContent().getContent(textStart, textEnd).toString()
						+ "=>" + normalString);
			}
		}
		DocumentRewriter rew = (DocumentRewriter) Factory.createResource("edu.columbia.gate.plugins.DocumentRewriter");
		rew.setDocument(doc);
		rew.setAnnotationSetName("");
		rew.execute();
		System.out.println(doc.getContent().toString());
		assertTrue("Found " + resultAS.size()
				+ " annotations instead of the expected 53!",
				resultAS.size() == 53);
		resultAS.clear();

		//test with partial words
		gaz.setWholeWordsOnly(false);
		gaz.execute();
		assertTrue("Found " + resultAS.size()
				+ " annotations instead of the expected 135!",
				resultAS.size() == 135);
		gaz.setWholeWordsOnly(true);
		resultAS.clear();

		//test with prefix matching
		gaz.setLongestMatchOnly(false);
		gaz.execute();
		assertTrue("Found " + resultAS.size()
				+ " annotations instead of the expected 68!",
				resultAS.size() == 68);
		gaz.setLongestMatchOnly(true);
		resultAS.clear();
		Factory.deleteResource(gaz);
		Factory.deleteResource(doc);
	}

	protected class AnnotationComparator implements Comparator<Annotation>
	{
		@Override
		public int compare(Annotation o1, Annotation o2)
		{
			int textStart1 = ((SimpleAnnotation) o1).getStartNode().getOffset().intValue();
			int textStart2 = ((SimpleAnnotation) o2).getStartNode().getOffset().intValue();
			if(textStart1 > textStart2)
				return 1;
			else if(textStart1 < textStart2)
				return -1;
			else
				return 0;
//			int textLength1 = ((String) ((SimpleAnnotation) o1).getFeatures().get("entryMatched")).length();
//			int textLength2 = ((String) ((SimpleAnnotation) o2).getFeatures().get("entryMatched")).length();
//			if(textLength1 < textLength2)
//				return 1;
//			else if(textLength1 > textLength2)
//				return -1;
//			else
//				return 0;
		}
		
	}
	/** Test suite routine for the test runner */
	public static Test suite()
	{
		return new TestSuite(TestEditDistanceGazetteer.class);
	} // suite

	public static void main(String[] args)
	{
		try
		{
			Gate.init();
			TestEditDistanceGazetteer testGaz = new TestEditDistanceGazetteer(
					"");
			testGaz.setUp();
			testGaz.testEditDistanceGazetteer();
			testGaz.tearDown();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	} // main

} // TestEditDistanceGazetteer
